package com.eastfair.venueservice.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.request.PageParams;
import com.eastfair.common.api.InvoiceApplyFeign;
import com.eastfair.common.api.InvoiceFeign;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.pay.demand.InvoiceApplyGroup;
import com.eastfair.pay.dto.InvoiceApplyDTO;
import com.eastfair.pay.dto.InvoiceApplyPageQuery;
import com.eastfair.pay.dto.InvoiceDTO;
import com.eastfair.pay.vo.InvoiceApplyVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @ClassNameInvoiceController
 * @Description 综合服务发票管理
 * @Authoradmin
 * @Date 2023/2/6 10:19
 * @Version 1.0
 **/
@Slf4j
@Validated
@RestController
@RequestMapping("/invoice")
@Api(value = "InvoiceController", tags = "发票")
@PreAuth(replace = "common:invoice:")
public class InvoiceController {

    /**
     * 发票申请service
     */
    @Autowired
    InvoiceApplyFeign invoiceApplyFeign;

    /**
     * 发票service
     */
    @Autowired
    InvoiceFeign invoiceFeign;
    /**
     * 发票申请保存
     */
    @SysLog("添加发票申请")
    @PostMapping("/saveInvoiceApply")
    public R saveInvoiceApply(@RequestBody InvoiceApplyDTO invoiceApplyDTO) {
        R r = invoiceApplyFeign.saveInvoiceApply(invoiceApplyDTO);
        if (r.getIsSuccess()) {
            return r;
        } else {
            throw BizException.wrap(r.getCode(), r.getErrorMsg());
        }
    }

    /**
     * 发票申请查询
     */
    @PostMapping("/invoiceApplyQueryList")
    public R<Page<InvoiceApplyVO>> invoiceApplyQueryList(@RequestBody @Validated PageParams<InvoiceApplyPageQuery> params) {
        R<Page<InvoiceApplyVO>> r = invoiceApplyFeign.queryList(params);
        if (r.getIsSuccess()) {
            return r;
        } else {
            throw BizException.wrap(r.getCode(), r.getErrorMsg());
        }
    }

    /**
     * 确认开票详情查询
     * @param invoiceApplyId
     * @return
     */
    @GetMapping("invoiceApplyCnfirmQueryById")
    @BaseControllerInfo(voGroup = {InvoiceApplyGroup.invoiceApplyCnfirmQueryById.class})
    public R<InvoiceApplyVO>  invoiceApplyCnfirmQueryById(@RequestParam("invoiceApplyId") @Validated @NotNull(message = "发票ID不能为为空") Long invoiceApplyId){
        log.info("结算详情查询 入参 invoiceApplyId {} ",invoiceApplyId);

        return invoiceApplyFeign.invoiceApplyCnfirmQueryById(invoiceApplyId);
    }


    /**
     * 确认开票
     */
    @SysLog("确认开票")
    @PostMapping("/saveInvoicecCnfirm")
    public R saveInvoicecCnfirm(@RequestBody List<InvoiceDTO> invoiceDTO) {

        return invoiceFeign.saveInvoicecCnfirm(invoiceDTO);
    }


    /**
     * 开票详情查询
     * @param invoiceApplyId
     * @return
     */
    @GetMapping("invoiceApplyQueryById")
    @BaseControllerInfo(voGroup = {InvoiceApplyGroup.invoiceApplyVOQueryById.class})
    public R<InvoiceApplyVO>  invoiceApplyQueryById(@RequestParam("invoiceApplyId") @Validated @NotNull(message = "发票ID不能为为空") Long invoiceApplyId){
        log.info("结算详情查询 入参 invoiceApplyId {} ",invoiceApplyId);

        return  invoiceApplyFeign.invoiceApplyQueryById(invoiceApplyId);
    }

}
